package com.lingnan.service.impl;

import cn.dev33.satoken.secure.SaSecureUtil;
import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;

import com.alibaba.nacos.shaded.com.google.gson.Gson;
import com.lingnan.entity.po.UserAccount;
import com.lingnan.enums.AuthUserStatusEnum;
import com.lingnan.mapper.AuthUserDao;
import com.lingnan.redis.RedisUtil;
import com.lingnan.service.AuthUserService;
import jakarta.annotation.Resource;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class AuthUserServiceImpl implements AuthUserService {

    @Resource
    private AuthUserDao authUserService;

    /*@Resource
    private AuthUserRoleDao authUserRoleService;

    @Resource
    private AuthPermissionDao authPermissionService;

    @Resource
    private AuthRolePermissionDao authRolePermissionService;

    @Resource
    private AuthRoleDao authRoleService;*/
//
    private String salt = "hhj";

    @Resource
    private RedisUtil redisUtil;

//    private String authPermissionPrefix = "auth.permission";

    private String authRolePrefix = "auth.role";

    private static final String LOGIN_PREFIX = "loginCode";

    private static final String PHONE_PREFIX = "phoneCode";

    @SneakyThrows
    @Transactional(rollbackFor = Exception.class)
    public Boolean register(UserAccount userAccount,String role) {

        List<UserAccount> existUser = authUserService.queryAllByLimit(userAccount);
        if (existUser.size() > 0) {
            return true;
        }
        if (StringUtils.isNotBlank(userAccount.getPassword())) {
            userAccount.setPassword(SaSecureUtil.md5BySalt(userAccount.getPassword(), salt));
        }
        if (StringUtils.isBlank(userAccount.getAvatar())) {
            userAccount.setAvatar("http://117.72.10.84:9000/user/icon/微信图片_20231203153718(1).png");
        }
        if (StringUtils.isBlank(userAccount.getNickName())) {
            userAccount.setNickName("两年半的求职生");
        }
        userAccount.setStatus(AuthUserStatusEnum.OPEN.getCode());
//        userAccount.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
        switch (role) {
            case "resume" -> userAccount.setUserType(0);
            case "company" -> userAccount.setUserType(1);
            case "admin" -> userAccount.setUserType(2);
            case "hr" -> userAccount.setUserType(3);
        }
        userAccount.setStatus(1);
        Integer count = authUserService.insert(userAccount);
        return count > 0;
    }

/*    @Override
    public Boolean update(AuthUser authUser) {
        Integer count = authUserService.updateByUserName(authUser);
        return count > 0;
    }

    @Override
    public Boolean delete(AuthUser authUserBO) {
        AuthUser authUser = new AuthUser();
        authUser.setId(authUserBO.getId());
        authUser.setIsDeleted(IsDeletedFlagEnum.DELETED.getCode());
        Integer count = authUserService.update(authUser);
        //有任何的更新，都要与缓存进行同步的修改
        return count > 0;
    }*/

    @Override
    public SaTokenInfo doLogin(String validCode,String role) {
        String loginKey = redisUtil.buildKey(LOGIN_PREFIX, validCode);
        String openId = (String) redisUtil.get(loginKey);
        if (StringUtils.isBlank(openId)) {
            return null;
        }
        UserAccount userAccount = new UserAccount();
        userAccount.setUsername(openId);
        this.register(userAccount,role);
        List<UserAccount> userAccounts = authUserService.queryAllByLimit(userAccount);
        StpUtil.login(userAccounts.get(0).getId());
        return StpUtil.getTokenInfo();
    }

    @Override
    public SaTokenInfo emailDoLogin(String email, String code) {
        String loginKey = redisUtil.buildKey(PHONE_PREFIX, code);
        String redisPhone = (String) redisUtil.get(loginKey);
        if (StringUtils.isBlank(redisPhone)||!StringUtils.equals(email,redisPhone)) {
            return null;
        }
        //todo，可拓展邮箱注册
        UserAccount userAccount = new UserAccount();
        userAccount.setEmail(email);
        List<UserAccount> userAccounts = authUserService.queryAllByLimit(userAccount);
        StpUtil.login(userAccounts.get(0).getId());
        return StpUtil.getTokenInfo();
    }

    @Override
    public SaTokenInfo adminDoLogin(String username, String password) {
        UserAccount userAccount = new UserAccount();
        userAccount.setUsername(username);
        userAccount.setPassword(password);
        List<UserAccount> userAccounts = authUserService.queryAllByLimit(userAccount);
        if (userAccounts.size()>0&&userAccounts.get(0).getUserType()==2){
            StpUtil.login(userAccounts.get(0).getId());
        }
        return StpUtil.getTokenInfo();
    }

   /* @Override
    public AuthUser getUserInfo(AuthUser authUserBO) {
        AuthUser authUser = new AuthUser();
        authUser.setUserName(authUserBO.getUserName());
        List<AuthUser> userList = authUserService.queryAllByLimit(authUser);
        if (CollectionUtils.isEmpty(userList)) {
            return new AuthUser();
        }
        return userList.get(0);
    }

    @Override
    public List<AuthUser> listUserInfoByIds(List<String> userNameList) {
        List<AuthUser> userList = authUserService.listUserInfoByIds(userNameList);
        if (CollectionUtils.isEmpty(userList)) {
            return Collections.emptyList();
        }

        return userList;
    }*/

}
